package com.ruoyi.web.controller.util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.aliyun.oss.OSSClient;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.ruoyi.common.config.Global;
import com.ruoyi.util.SerialNumber;
import org.springframework.util.ResourceUtils;

//生成二维码，返回访问地址
public class CreQcode {

    public static void main(String[] args) throws Exception {
         SerialNumber serial = new SerialNumber("SK");
//        CreQcode creQcode=new CreQcode();
        String order = serial.getAtomicNumber();
        String url = CreQcode.showURL(order);
        System.out.println(url);
    }



    public static String showURL(String order) throws IOException {
        // 第一种：获取类加载的根路径   D:\git\daotie\daotie\target\classes
        String path = Global.getProfile();
        String url=null;
        try {
            OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = multiFormatWriter.encode(order, BarcodeFormat.QR_CODE, 400, 400, hints);
            File file1 = new File(path, "qcode.jpg");
            //生成二维码图片
            MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
            url = AliyunOSSClientUtil.uploadObject2OSS(ossClient, file1, AliyunOSSClientUtil.BACKET_NAME, AliyunOSSClientUtil.FOLDER);
            System.out.println(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }
}